package com.zaofan.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Dynamic;
import com.zaofan.bean.DynamicDTO;
import com.zaofan.bean.PageBean;
import com.zaofan.service.DynamicService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

@WebServlet("/admin/topicReply")
public class TopicReplyController extends BaseServlet {
	private static final long serialVersionUID = -5377620282223515871L;
	private DynamicService replyService = new DynamicService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			if (request.getParameter("topicId") == null) {
				response.getWriter().println("非法访问");
				return;
			}
			int topicId = 0;
			try {
				topicId = Integer.valueOf(request.getParameter("topicId"));
			} catch (Exception e) {
				response.getWriter().println("非法访问");
				return;
			}
			String url = "topicReply?action=list&topicId=" + topicId;
			Integer begin = 1;
			try {
				begin = Integer.valueOf(request.getParameter("begin"));
			} catch (Exception e) {
				begin = 1;
			}

			Integer end = 10;
			try {
				end = Integer.valueOf(request.getParameter("end"));
			} catch (Exception e) {
				end = 10;
			}

			String replyType = request.getParameter("replyType");
			String type = request.getParameter("type");
			PageBean pageBean = new PageBean();
			int page = begin - 1;
			pageBean.setBegin(page * 10 + 1);
			List<DynamicDTO> topics = replyService.findByTopicIdWithOther((page * 10) + "", end + "", topicId + "",
					replyType, type);
			Long count = replyService.countByTopicIdWithOther(topicId + "", replyType, type);
			pageBean.setEnd(page * 10 + topics.size());
			pageBean.setTotal(count.intValue());
			pageBean.setPageInfo(WebUtils.getPageData(count.intValue(), 10, page + 1, url));
			request.setAttribute("replys", topics);
			request.setAttribute("pageBean", pageBean);
			request.getRequestDispatcher("/admin/topic/topicReplyList.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Dynamic topicReply = replyService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", topicReply, response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("查询失败!!", response);
	}

	protected void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Dynamic topicReply = replyService.findById(id);
			replyService.delReply(topicReply);
			JsonUtils.writeSuccessJson("删除成功!!", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("删除失败!!", response);
	}

	protected void state(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Dynamic topicReply = replyService.findById(id);
			topicReply.setType(topicReply.getType() == 1 ? 2 : 1);
			replyService.update(topicReply);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("更新失败!!", response);
	}

}
